﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using Common;
using Model;

namespace CodeFactory
{
    public partial class MethodFieldCreater : Form
    {


        #region 私有字段
        private CustomPropertyGrid cpg;
        private enumPropertyType enumPT;

        private bool IsFromCodeFactary=false;
        private CodeShop cf;
        #endregion


        #region 初始化
        public MethodFieldCreater()
        {
            InitializeComponent();
        }

        public MethodFieldCreater(CodeShop _cf,bool _IsFromCodeFactary)
        {
            InitializeComponent();
            cf = _cf;
            IsFromCodeFactary = _IsFromCodeFactary;
            enumPT = enumPropertyType.Method;
            cmbType.Enabled = false;
            txtAttribute.Enabled = false;
        }

        public MethodFieldCreater(enumPropertyType _enumPT, CustomPropertyGrid _cpg)
        {
            InitializeComponent();
            cpg = _cpg;
            enumPT = _enumPT;
        }

        private void MethodFieldCreater_Load(object sender, EventArgs e)
        {
            InitializeComboboxSelected();

        }
        #endregion


        #region 私有方法

        /// <summary>
        /// 清楚参数表
        /// </summary>
        private void CleanParameterInfo()
        {
            treePatameter.Nodes.Clear();
            cmbParameterOutput.Text = "";
            cmbParameterType.Text = "";
            txtParameterName.Text = "";
        }

        private void InitializeComboboxSelected()
        {
            switch(enumPT)
            {
              case enumPropertyType.Field:           cmbType.SelectedIndex = 0;   break;
              case enumPropertyType.Method:          cmbType.SelectedIndex = 1;   break;
              case enumPropertyType.CustomProperty:  cmbType.SelectedIndex = 2;   break;
              case enumPropertyType.CustomEvent:     cmbType.SelectedIndex = 3;   break;
            }
            //cmbType.Enabled = false;
            cmbParameterOutput.SelectedIndex = 0;
            cmbAccessibility.SelectedIndex = 0;
        }

        private bool ValidationParamHasEmpty()
        {
            if(String.IsNullOrEmpty(cmbParameterType.Text.Trim()))
            {
                MessageBox.Show(lblParameterType.Text+"不能为空！");
                return true;
            }
            if(String.IsNullOrEmpty(txtParameterName.Text.Trim()))
            {
                MessageBox.Show(lblParameterName.Text+"不能为空！");
                return true;
            }
            return false;    
        }

        private bool ValidationMainParamEmpty()
        {   
            if (String.IsNullOrEmpty(cmbAccessibility.Text.Trim()))
            {
                MessageBox.Show(lblAccessibility.Text + "不能为空！");
                return true;
            } 
            if (String.IsNullOrEmpty(cmbReturnType.Text.Trim()))
            {
                MessageBox.Show(lblReturnType.Text + "不能为空！");
                return true;
            }
            if (String.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show(lblName.Text + "不能为空！");
                return true;
            }
            if (enumPT == enumPropertyType.CustomEvent)
            {
                if (String.IsNullOrEmpty(cmbEventArgs.Text.Trim()))
                {
                    MessageBox.Show(lblEventArgs.Text + "不能为空！");
                    return true;
                }
            }
            return false;    
        }

        private List<eMethodParam> GetParamListFromTree()
        {
            List<eMethodParam> list = new List<eMethodParam>();

            if (treePatameter.Nodes.Count < 1) 
                return list;
          
            foreach (TreeListNode tln in treePatameter.Nodes)
            {
                eMethodParam emp = new eMethodParam();
                emp.ParamName = tln.GetValue(0) == null ? "" : tln.GetValue(0).ToString().Trim();
                emp.DataType = tln.GetValue(1) == null ? "" : tln.GetValue(1).ToString().Trim();
                emp.OutputType = tln.GetValue(2) == null ? "" : tln.GetValue(2).ToString().Trim();
                list.Add(emp);
            }
            return list;
        }
        #endregion


        #region 按钮事件
        
        private void btnNewParameter_Click(object sender, EventArgs e)
        {
            gbParameter.Visible = false;
            gbNewParameter.Visible = true;
        }

        private void btnParameter_Click(object sender, EventArgs e)
        {
            gbParameter.Visible = true;
            gbNewParameter.Visible = false;
        }

        private void btnParameterOK_Click(object sender, EventArgs e)
        {
            if (ValidationParamHasEmpty()) return;

            TreeListNode tln = treePatameter.AppendNode(null, null);
            tln.SetValue(0, txtParameterName.Text.Trim());
            tln.SetValue(1,cmbParameterType.Text.Trim());
            tln.SetValue(2, cmbParameterOutput.Text.Trim());

            cmbParameterType.Text = "";
            txtParameterName.Text = "";
            cmbParameterOutput.Text = "";

            gbParameter.Visible = true;
            gbNewParameter.Visible = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ValidationMainParamEmpty()) return;

            eProperty ep = new eProperty();

            ep.PropertyType = (enumPropertyType)Enum.Parse(typeof(enumPropertyType), cmbType.Text.Trim(), false);
            ep.Accessibility = cmbAccessibility.Text.Trim();
            ep.ReturnType = cmbReturnType.Text.Trim();
            ep.Name = txtName.Text.Trim();
            ep.Attribute = txtAttribute.Text.Trim();
            ep.EventArgs = cmbEventArgs.Text.Trim();
            ep.ParamList = GetParamListFromTree();

            if (IsFromCodeFactary)
            {
                cf.AddNewMethod(ep);
            }
            else
            {

                cpg.AddMethodsToTree(ep);
            }
            this.Close();
        }


        #endregion


        #region 其他事件

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbType.SelectedIndex)
            {
                case 0://Field
                    CleanParameterInfo();
                    cmbReturnType.Items.Remove("void");
                    this.MaximumSize = new System.Drawing.Size(250, 266);
                    this.MinimumSize = new System.Drawing.Size(250, 266);
                    gbParameter.Visible = false;
                    gbNewParameter.Visible = false;
                    btnNewParameter.Enabled = false;
                    cmbEventArgs.Enabled = false;
                    break;

                case 1://Method

                    CleanParameterInfo();
                    cmbReturnType.Items.Add("void");
                    cmbReturnType.SelectedItem = "void";
                    this.MaximumSize = new System.Drawing.Size(0, 266);
                    this.MinimumSize = new System.Drawing.Size(550, 266);
                    gbParameter.Visible = true;
                    gbNewParameter.Visible = false;
                    btnNewParameter.Enabled = true;
                    cmbEventArgs.Enabled = false;
                    break;

                case 2://CustomProperty
                    CleanParameterInfo();
                    cmbReturnType.Items.Remove("void");
                    this.MaximumSize = new System.Drawing.Size(250, 266);
                    this.MinimumSize = new System.Drawing.Size(250, 266);
                    gbParameter.Visible = false;
                    gbNewParameter.Visible = false;
                    btnNewParameter.Enabled = false;
                    cmbEventArgs.Enabled = false;
                    break;

                case 3://CustomEvent
                    CleanParameterInfo();
                    cmbReturnType.Items.Remove("void");
                    this.MaximumSize = new System.Drawing.Size(250, 266);
                    this.MinimumSize = new System.Drawing.Size(250, 266);
                    gbParameter.Visible = false;
                    gbNewParameter.Visible = false;
                    btnNewParameter.Enabled = false;
                    cmbEventArgs.Enabled = true;
                    break;
            }
        }


        #endregion

       

     }
}
